package gui;

import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Image;

import javax.imageio.ImageIO;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JButton;


import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import javax.swing.JScrollPane;

import Announce.ThongBaoForm;
import BoBai.BoBais;
import BoBai.DanhSachBoBaiDaSX;
import BoBai.NguoiChoi;
import Chat.ChatForServer;
import Chat.ThreadNhanTinHieuChat2;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Connect extends JFrame {
	private static final long serialVersionUID = 1L;
	
	public Connect cc;
	
	public ServerSocket ls;
	public ThreadLangNghe olt;

	public static ClientInfo ci;
	
	public JPanel contentPane;
	public JButton btnStartServer;
	public JButton btnStopServer;
	public MoHinhTable mht;
	public JTable table;
	public JScrollPane scrollPane;
	private JButton btnThgBao;
	public JButton btnChat ;
	 JFileChooser fc;
	
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Connect frame = new Connect();
					frame.setVisible(true);
					
					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Connect() {
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosed(WindowEvent arg0) {
				KetThucLangNghe();
			}
		});
		setTitle("Lan Support");
		cc = this;

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(410, 294);
		setLocationRelativeTo(null);
		setResizable(false);
		contentPane = new JPanel();
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		mht = new MoHinhTable();		
		
		// Start Server
		btnStartServer = new JButton("Start Server");
		btnStartServer.setBounds(20, 11, 106, 23);
		btnStartServer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				BatDauLangNghe();
			}
		});		
		contentPane.add(btnStartServer);
		
		btnStopServer = new JButton("Stop Server");
		btnStopServer.setBounds(266, 11, 116, 23);
		btnStopServer.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				KetThucLangNghe();
			}
		});
		contentPane.add(btnStopServer);
		
		table = new JTable(mht);
		
		scrollPane = new JScrollPane(table);
		scrollPane.setBounds(20, 45, 363, 172);
		contentPane.add(scrollPane);
		
		btnThgBao = new JButton("Th\u00F4ng B\u00E1o");
		btnThgBao.setBounds(20, 228, 111, 23);
		btnThgBao.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				ThongBaoForm tbf = new ThongBaoForm(cc);
				tbf.setVisible(true);
			}
		});
		contentPane.add(btnThgBao);
		
		btnChat = new JButton("Chat");
		btnChat.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int k = -1;
				k = table.getSelectedRow();
				if(k > -1)
				{
					for(ClientInfo ci : cc.mht.dsClient) {
						//String[] s = ci.chatSocket.getInetAddress().toString().split("/");
						if(ci.chatSocket.getInetAddress().getHostAddress().equals( (String) table.getModel().getValueAt(k, 1)) && ci.servicePort == Integer.parseInt( table.getModel().getValueAt(k, 3).toString()))
						{						
							ci.chatform = new ChatForServer( ci);
							ci.chatform.setVisible(true);
							if(ci.threadNhanTHChat == null)
							{
								ci.threadNhanTHChat = new ThreadNhanTinHieuChat2(ci);
								ci.threadNhanTHChat.start();
							}
							break;
							
						}
					}
				}else
				{
					JOptionPane.showMessageDialog(cc, "Phải chọn 1 clien để chat !");
				}
				
			}
		});
		btnChat.setBounds(291, 228, 91, 23);
		contentPane.add(btnChat);
	}
	
	private void BatDauLangNghe() {
		try {
			//int port = Integer.parseInt(JOptionPane.showInputDialog("Insert Server Port :"));
			int port = 1234;
			ls = new ServerSocket(port);
		} catch(NumberFormatException nfe) {
			JOptionPane.showMessageDialog(null, "Wrong integer number format!");
			return;
		} catch (IOException e) {
			JOptionPane.showMessageDialog(null, "Cannot open server socket!");
			return;
		}		
		
		// Start listen thread
		olt = new ThreadLangNghe(ls, this);
		olt.start();
		
		// GUI
		btnStartServer.setEnabled(false);
		
		//sap xep bo bai de chuan bi chuyen xuong
		BoBais bb = new BoBais();
		for(int i=0; i<4; i++){
			NguoiChoi hand = new NguoiChoi(bb); 
			hand.SapBai();
			for(int j=0; j<13; j++){
				String s = hand.dslabai[j].getName();
				DanhSachBoBaiDaSX.DSBoBaiSX.add(s);
			}
		}
	}
	
	private void KetThucLangNghe() {
		int clients = mht.getRowCount();
		if(clients > -1) {
			for(int i = clients-1; i >= 0; i--) {
				ClientInfo ci = mht.getRow(i);
				
				
				
				try {
					
					ci.announceSocket.close();
					ci.chatSocket.close();
					
					ci.controlSocket.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
